<template>
  <div class="index-container">
    <!-- 头部搜索栏区域 -->
    <van-nav-bar class="index-header-search">
      <van-button round slot="title" plain type="info" to="/search">
        <i class="newsfont newssousuo"></i>
        搜索
      </van-button>
    </van-nav-bar>
    <!-- 汉堡导航 -->
    <div class="index-header-more" @click="isShowPopupBool = true">
      <i class="newsfont newsgengduo"></i>
    </div>
    <!-- 滑动Tab标签区域 -->
    <van-tabs v-model="activeTab" title-inactive-color="#777777" title-active-color="#333333" sticky animated swipeable border class="index-nav-tabs">
      <van-tab v-for="channels in userChannelsData" :key="channels.id" :title="channels.name">
        <article-list :channels="channels"></article-list>
      </van-tab>
      <div slot="nav-right" class="tab-placeholder"></div>
    </van-tabs>
    <!-- 弹出层 -->
    <van-popup
      v-model="isShowPopupBool"
      round
      closeable
      close-icon="close"
      close-icon-position="top-left"
      position="bottom"
      :style="{ height: '100%' }">
      <channel-edit
        :activeNum="activeTab"
        :userChannels="userChannelsData"
        @addMyChannel="addMyChannelhandle"
        @toggleActive="toggleActivehandle"
        @closePopup="isShowPopupBool = false"
        @updateMyChannelList="updateMyChannelListhandle"
      ></channel-edit>
      </van-popup>
  </div>
</template>

<script>
// 导入接口请求
import {
  getUserChannels,
  addUserChannels,
  removeUserChannels
} from '@/api/users'
// 导入频道文章列表组件
import ArticleList from '@/views/home/components/article-list.vue'
// 导入编辑频道内部布局组件
import channelEdit from './components/channel-edit'
// 导入VueX仓库数据
import { mapState } from 'vuex'
// 导入本地存储方法
import {
  setLocalStorageItem,
  getLocalStorageItem
} from '@/utils/storage'
export default {
  name: 'Index',
  components: {
    'article-list': ArticleList,
    'channel-edit': channelEdit
  },
  computed: {
    ...mapState(['userDataServe'])
  },
  data () {
    return {
      // 当前用户频道列表信息
      userChannelsData: [],
      // 弹出层显示隐藏
      isShowPopupBool: false,
      // Tab栏展示项Index
      activeTab: 0
    }
  },
  created () {
    // 获取当前用户频道列表
    this.loadChannels()
  },
  methods: {
    // 页面加载获取当前用户频道列表分类
    async loadChannels () {
      try {
        // 已登录用户请求获取频道数据
        if (this.userDataServe) {
          const { data: res } = await getUserChannels()
          this.userChannelsData = res.data.channels
        } else {
        // 未登录用户从LocalStorage中获取
          const localChannelData = getLocalStorageItem('channels')
          if (localChannelData) {
            this.userChannelsData = localChannelData
          } else {
            const { data: res } = await getUserChannels()
            this.userChannelsData = res.data.channels
          }
        }
      } catch (err) {
        this.$toast('用户数据异常，请稍后重新尝试刷新获取！')
      }
    },
    // 子组件传值要求增加用户频道
    async addMyChannelhandle (item) {
      this.userChannelsData.push(item)
      try {
        // 处理登录用户添加频道请求
        if (this.userDataServe) {
          await addUserChannels([{
            id: item.id,
            seq: this.userChannelsData.length
          }])
        } else {
          // 处理未登录用户添加频道请求,将数据存储到本地LocalStorage
          setLocalStorageItem('channels', this.userChannelsData)
        }
      } catch (err) {
        this.$toast.fail('添加频道失败,请稍后重新尝试!')
      }
    },
    // 子组件未在编辑状态切换Tab栏
    toggleActivehandle (index) {
      this.activeTab = index
    },
    // 子组件要求更新频道列表
    async updateMyChannelListhandle (itemArr) {
      const spliceInde = this.userChannelsData.findIndex(item => {
        return item.id === itemArr[0].id
      })
      try {
        this.userChannelsData.splice(spliceInde, 1)
        if (this.userDataServe) {
          await removeUserChannels(itemArr[0].id)
        } else {
          setLocalStorageItem('channels', this.userChannelsData)
        }
      } catch (err) {
        this.$toast.fail('数据异常,请稍后重试')
      }
    }
  }
}
</script>

<style lang="less" scoped>
/*顶部搜索栏*/
/deep/.van-nav-bar {
  height: 128px;
  .van-nav-bar__content {
    height: 100%;
    .van-nav-bar__title {
      max-width: 100%;
    }
  }
  .van-button {
    border: none;
    margin: 52px 0 12px 0;
    width: 555px;
    height: 64px;
    background-color: #5BABFB;
    color: #fff;
    font-size: 28px;
    .newsfont {
      font-size: 32px;
    }
  }
}
/*汉堡导航字体图标*/
.newsgengduo {
  position: fixed;
  right: 0;
  top: 128px;
  z-index: 1;
  width: 66px;
  font-size: 44px;
  font-weight: 500;
  text-align: center;
  line-height: 82px;
  height: 82px;
  background-color: #fff;
  opacity: .7;
  box-sizing: border-box;
}
/*汉堡导航伪元素竖线*/
.index-header-more::before {
  content: "";
  position: absolute;
  right: 68px;
  width: 2px;
  height: 56px;
  top: 140px;
  background: url('~@/assets/gradient-gray-line.png') no-repeat;
  z-index: 2;
  opacity: .5;
}
/*滑动标签*/
/deep/.index-nav-tabs {
  .van-tabs__wrap {
    height: 83px;
    .van-tabs__nav {
      padding: 0;
      >.van-tab:first-child {
        >span {
          font-size: 30px;
        }
      }
      .van-tab {
      width: 192px;
      font-size: 28px;
      border-left: 1px solid #edeff3;
      border-bottom: 1px solid #edeff3;
      }
      .van-tabs__line {
        bottom: 11px;
        height: 6px;
        width: 31px;
        border-radius: 3px;
        background-color: #3296fa;
      }
    }
  }
}
.tab-placeholder {
  flex-shrink: 0;
  width: 66px;
  height: 100%;
}
/deep/.van-popup {
  .van-icon {
    color: black;
  }
}

</style>
